import { queryNews } from './service';
const R = require('ramda');

const Model = {
  namespace: 'newsDetail',
  state: {
    list: [],
    content: {},
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryNews, payload);

      if (response.resultCode === 'ok') {
        if (!R.isNil(response.content) && !R.isEmpty(response.content)) {
          let content =
            R.isNil(response.content.content) || R.isEmpty(response.content.content)
              ? ''
              : response.content.content;
          response.content.content = content.replace(
            /<img src="/g,
            '<img src="' + PORTAL_PATH + '/',
          );
        }

        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    appendList(state = { list: [] }, action) {
      return { ...state, list: state.list.concat(action.payload) };
    },
  },
};
export default Model;
